<template>
  <div class="app-container">

    <!-- 查询操作 -->
    <el-form :inline="true" class="filter-container filter">
      <el-form-item label="课题组名称">
        <el-input v-model="listQuery.ftaskname" clearable class="filter-item" style="width: 200px;"
          placeholder="请输入课题组名称" />
      </el-form-item>
      <el-form-item label="团队">
        <el-select v-model="listQuery.fteamno" clearable filterable class="filter-item" placeholder="请选择团队">
          <el-option v-for="item in fteamnoList" :key="item.fteamno" :label="item.fteamname" :value="item.fteamno" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button round size="medium" class="filter-item"
          :style="{ background: buttonColor.lookup, color: buttonColor.color }" @click="handleFilter">查找</el-button>
        <el-button round size="medium" class="filter-item"
          :style="{ background: buttonColor.add, color: buttonColor.color }" @click="handleCreate">添加</el-button>
      </el-form-item>
    </el-form>

    <!-- 查询结果 -->
    <el-table :height="buttonColor.clientHeight" v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。"
      border fit highlight-current-row :header-cell-style="tableColor">
      <el-table-column align="center" :width="buttonColor.operationTwo" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <div class="operationButton">
            <div :style="{ color: buttonColor.edit, cursor: 'pointer' }" @click="handleUpdate(scope.row)">
              <img src="../../../public/images/edit.png" />
              编辑
            </div>
            <div :style="{ color: buttonColor.delete, cursor: 'pointer' }" @click="handleDelete(scope.row)">
              <img src="../../../public/images/del.png" />
              删除
            </div>
            <div :style="{ color: buttonColor.person, cursor: 'pointer' }" @click="person(scope.row)">
              <img src="../../../public/images/person.png" />
              人员
            </div>
            <div :style="{ color: buttonColor.lab, cursor: 'pointer' }" @click="lab(scope.row)">
              <img src="../../../public/images/lab.png" />
              实验室
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column :width="buttonColor.line" :index="indexMethod" align="center" label="序" type="index" />
      <el-table-column :formatter="fteamnoFormatter" :width="buttonColor.name" align="left" label="团队" prop="fteamno" />
      <el-table-column :width="buttonColor.name" align="left" label="课题组编号" prop="ftaskno" />
      <el-table-column width="130" align="left" label="课题组名称" prop="ftaskname" />
      <el-table-column :formatter="fpersonFormatter" align="left" label="负责人" prop="fperson" />
      <el-table-column align="left" :width="buttonColor.state" label="状态" prop="fstate">
        <template slot-scope="scope">
          <span :style="{ color: buttonColor.statedisable }" v-if="scope.row.fstate === 0">{{
              scope.row.fstate | statusFilter
          }}</span>
          <span :style="{ color: buttonColor.statestart }" v-else>{{ scope.row.fstate | statusFilter }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog width="auto" top="0vh" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      <template slot="title">
        <div :style="titleColor">{{ textMap[dialogStatus] }}</div>
      </template>
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="auto">
        <el-row>
          <el-col :span="12">
            <el-form-item class="required" label="团队" prop="fteamno">
              <el-select v-if="dialogStatus == 'create'" v-model="dataForm.fteamno" clearable filterable class="filter-item" placeholder="请选择团队">
                <el-option v-for="item in fteamnoList" :key="item.fteamno" :label="item.fteamname"
                  :value="item.fteamno" />
              </el-select>
              <el-select v-else disabled v-model="dataForm.fteamno" clearable filterable class="filter-item" placeholder="请选择团队">
                <el-option v-for="item in fteamnoList" :key="item.fteamno" :label="item.fteamname"
                  :value="item.fteamno" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item class="required" label="状态" prop="attribute">
              <el-select v-model="dataForm.fstate" clearable filterable class="filter-item" placeholder="请选择状态">
                <el-option v-for="item in fstateList" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item class="required" label="课题组编号" prop="ftaskno">
              <el-input v-if="dialogStatus == 'create'" v-model="dataForm.ftaskno" />
              <el-input v-else disabled v-model="dataForm.ftaskno" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item class="required" label="课题组名称" prop="ftaskname">
              <el-input v-model="dataForm.ftaskname" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item class="searchOne required" label="责任人" prop="fperson">
              <div style="display:flex;justify-content:flex-start;align-items:center">
                <el-select style="display: inline-block" clearable filterable v-model="dataForm.fperson"
                  class="filter-item" placeholder="请选择责任人">
                  <el-option v-for="item in getListforduserAdd" :key="item.value" :label="item.label"
                    :value="item.value" />
                </el-select>
                <i class="el-icon-search selectI" @click="searchDialoguse('add')" />
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item style="width: 100% !important" class="text" label="备注" prop="fmemo">
              <el-input v-model="dataForm.fmemo" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button round :style="{ background: buttonColor.cancel, color: buttonColor.colorone }"
          @click="dialogFormVisible = false">取消</el-button>
        <el-button round v-if="dialogStatus == 'create'"
          :style="{ background: buttonColor.confirm, color: buttonColor.color }" @click="createData">确定</el-button>
        <el-button round v-else :style="{ background: buttonColor.confirm, color: buttonColor.color }"
          @click="updateData">确定
        </el-button>
      </div>
    </el-dialog>

    <!-- 人员添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="dialogPersonVisible" width="80%">
      <template slot="title">
        <div :style="titleColor">{{ textMap[dialogStatus] }}</div>
      </template>
      <el-form ref="dataFormPerson" :model="dataForm" status-icon label-position="left"
        label-width="90px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="团队" prop="fteamno">
              <el-select disabled v-model="dataForm.fteamno" clearable filterable class="filter-item" placeholder="请选择团队">
                <el-option v-for="item in fteamnoList" :key="item.fteamno" :label="item.fteamname"
                  :value="item.fteamno" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="课题组编号" prop="ftaskno">
              <el-input v-model="dataForm.ftaskno" disabled type="number" />
              <!-- oninput="value=value.replace(/[^\d]/g,'')"-->
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="课题组名称" prop="ftaskname">
              <el-input disabled v-model="dataForm.ftaskname" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div :style="{ width: '100%', display: 'flex', border: '2px solid #F2F2F2', height: panelHeight }">
        <el-tabs style="width: 100%" type="border-card">
          <el-tab-pane label="人员列表">
            <div class="app-container">
              <!-- 子表查询和其他操作 -->
              <div class="filter-container">
                <!-- v-permission="['issue:takeinHead:create']" -->
                <el-button round size="medium" class="filter-item"
                  :style="{ background: buttonColor.add, color: buttonColor.color }" @click="handleUpdatePerson">添加
                </el-button>
              </div>
              <!-- 查询结果 -->
              <el-table :height="panelHeight" v-loading="listLoading" :data="personList" stripe
                element-loading-text="正在查询中。。。" border fit highlight-current-row :header-cell-style="tableColor">
                <el-table-column :index="indexMethod" align="center" label="序" type="index" />
                <el-table-column align="left" label="人员编号" prop="id" />
                <el-table-column :formatter="fusernoFormatter" align="left" label="人员名称" prop="username" />
                <el-table-column label="操作">
                  <template slot-scope="scope">
                    <div class="operationButton">
                      <div :style="{ color: buttonColor.delete, cursor: 'pointer' }" @click="handleDelete1(scope.row)">
                        <img src="../../../public/images/del.png" />
                        删除
                      </div>
                    </div>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button round :style="{ background: buttonColor.cancel, color: buttonColor.colorone }" @click="canclePerson">
          取消
        </el-button>
        <el-button round :style="{ background: buttonColor.confirm, color: buttonColor.color }"
          @click="updateDataPerson">确定
        </el-button>
      </div>
    </el-dialog>

    <!-- 人员添加 -->
    <el-dialog width="auto" top="0vh" :close-on-click-modal="false" :visible.sync="addPersonVisible">
      <template slot="title">
        <div :style="titleColor">{{ textMap[dialogStatus] }}</div>
      </template>
      <el-form ref="dataFormPersonSon" :model="dataFormPersonSon" status-icon label-position="left" label-width="auto">
        <el-row>
          <el-col :span="8">
            <el-form-item class="searchOne required" label="人员" prop="fuserno">
              <div style="display:flex;justify-content:flex-start;align-items:center">
                <el-select style="display: inline-block" clearable filterable v-model="dataFormPersonSon.fuserno"
                  class="filter-item" placeholder="请选择人员">
                  <el-option v-for="item in getListforduserAdd" :key="item.value" :label="item.label"
                    :value="item.value" />
                </el-select>
                <i class="el-icon-search selectI" @click="searchDialoguse('son')" />
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button round :style="{ background: buttonColor.cancel, color: buttonColor.colorone }"
          @click="addPersonVisible = false">取消</el-button>
        <el-button round :style="{ background: buttonColor.confirm, color: buttonColor.color }"
          @click="updateDataPersonSon">
          确定</el-button>
      </div>
    </el-dialog>

    <!-- 实验室添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="dialogLabVisible" width="80%">
      <template slot="title">
        <div :style="titleColor">{{ textMap[dialogStatus] }}</div>
      </template>
      <el-form ref="dataFormLab" :model="dataForm" status-icon label-position="left" label-width="90px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="团队" prop="fteamno">
              <el-select disabled v-model="dataForm.fteamno" clearable filterable class="filter-item" placeholder="请选择团队">
                <el-option v-for="item in fteamnoList" :key="item.fteamno" :label="item.fteamname"
                  :value="item.fteamno" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="课题组编号" prop="ftaskno">
              <el-input v-model="dataForm.ftaskno" disabled type="number" />
              <!-- oninput="value=value.replace(/[^\d]/g,'')"-->
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="课题组名称" prop="ftaskname">
              <el-input disabled v-model="dataForm.ftaskname" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div :style="{ width: '100%', display: 'flex', border: 'LabList2px solid #F2F2F2', height: panelHeight }">
        <el-tabs style="width: 100%" type="border-card">
          <el-tab-pane label="实验室列表">
            <div class="app-container">
              <!-- 子表查询和其他操作 -->
              <div class="filter-container">
                <!-- v-permission="['issue:takeinHead:create']" -->
                <el-button round size="medium" class="filter-item"
                  :style="{ background: buttonColor.add, color: buttonColor.color }" @click="handleUpdatelab">添加
                </el-button>
              </div>
              <!-- 查询结果 -->
              <el-table :height="panelHeight" v-loading="listLoading" :data="labListTamp" stripe
                element-loading-text="正在查询中。。。" border fit highlight-current-row :header-cell-style="tableColor">
                <el-table-column :index="indexMethod" align="center" label="序" type="index" />
                <el-table-column align="left" label="实验室编号" prop="fpointno" />
                <el-table-column :formatter="fpointnoFormatter" align="left" label="实验室名称" prop="fpointname" />
                <el-table-column label="操作">
                  <template slot-scope="scope">
                    <div class="operationButton">
                      <div :style="{ color: buttonColor.delete, cursor: 'pointer' }" @click="handleDelete2(scope.row)">
                        <img src="../../../public/images/del.png" />
                        删除
                      </div>
                    </div>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button round :style="{ background: buttonColor.cancel, color: buttonColor.colorone }" @click="cancleLab">取消
        </el-button>
        <el-button round :style="{ background: buttonColor.confirm, color: buttonColor.color }" @click="updateDataLab">
          确定
        </el-button>
      </div>
    </el-dialog>

    <!--实验室添加 -->
    <el-dialog width="auto" top="0vh" :close-on-click-modal="false" :visible.sync="addLabVisible">
      <template slot="title">
        <div :style="titleColor">{{ textMap[dialogStatus] }}</div>
      </template>
      <el-form ref="dataFormLabSon" :model="dataFormLabSon" status-icon label-position="left" label-width="auto">
        <el-row>
          <el-col :span="8">
            <el-form-item label="实验室">
              <div style="display:flex;align-items:center">
                <el-select style="display: inline-block;" clearable filterable v-model="dataFormLabSon.fpointno"
                  class="filter-item" placeholder="请选择实验室">
                  <el-option v-for="item in getListfpointNo" :key="item.value" :label="item.label"
                    :value="item.value" />
                </el-select>
                <i style="display: inline-block;width:20px;color:#409EFF;margin-left:3px" class="el-icon-search"
                  @click="searchDialoglab('add')" />
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button round :style="{ background: buttonColor.cancel, color: buttonColor.colorone }"
          @click="addLabVisible = false">取消</el-button>
        <el-button round :style="{ background: buttonColor.confirm, color: buttonColor.color }"
          @click="updateDataLabSon">
          确定</el-button>
      </div>
    </el-dialog>

    <userDialog :searchVisibleuse.sync="searchVisibleuse" :fsiteNo.sync="userDialogFsiteNo" :tableColor="tableColor"
      @userDialog="userDialogValue" />
    <labDialog :searchVisiblelab.sync="searchVisiblelab" :fsiteNo.sync="labDialogFsiteNo" :tableColor="tableColor"
      @labDialog="labDialogValue" />
  </div>
</template>

<style>
.button-margin {
  margin: 2px 0px;
}

.el-button+.el-button {
  margin-left: 1px;
}
</style>

<script>
/**
 * 引入api
 * 引入组件
 */
import { listtsTask, createtsTask, updatetsTask, deletetsTask } from '@/api/labOrder/teamTask'
import { listtsTaskuser, createtsTaskuser, deletetsTaskuser } from '@/api/labOrder/teamTaskUser'
import { listtsTaskpoint, createtsTaskpoint, deletetsTaskpoint } from '@/api/labOrder/teamTaskPoint'
import { listtsTeam } from '@/api/labOrder/team'
import { getListArea } from '@/api/area'
import UserDialog from '@/components/UserDialog'
import LabDialog from '@/components/LabDialog'
import Pagination from '@/components/Pagination'
import { tableColor, titleColor, buttonColor } from '@/filters/index'
import moment from 'moment'

const statusMap = {
  0: '停用',
  1: '正常'
}

export default {
  name: 'WTeamTask',
  // 使用组件
  components: { Pagination, UserDialog, LabDialog },
  // 表单状态过滤
  filters: {
    statusFilter(type) {
      return statusMap[type]
    }
  },
  data() {
    return {
      fteamnoList: [], // 团队列表
      searchVisiblelab: false, // 是否显示地点弹窗
      labDialogFsiteNo: null,// 客户编号(地点弹出框)
      labFlag: null, // 判断地点是搜索还是添加
      getListfpointNo: [], // 地点列表
      dataFormLabSon: {
        fpointno: undefined,
        fpointname: undefined,
      },
      addLabVisible: false, // 实验室弹窗添加框
      labListTamp: [], // 实验室查找列表
      dialogLabVisible: false, // 实验室弹窗
      dataFormPersonSon: {
        fuserno: undefined,
        fusername: undefined,
      },
      personList: [], // 人员查询列表
      addPersonVisible: false, // 人员弹窗添加框
      dialogPersonVisible: false, // 人员弹窗

      userFlag: null, // 判断人员是搜索还是添加
      searchVisibleuse: false, // 是否显示人员弹窗
      userDialogFsiteNo: null, // 客户编号(人员弹出框)
      getListforduserAdd: [], // 添加人员列表
      panelHeight: 0,
      fstateList: [
        { label: '正常', value: 1 },
        { label: '停用', value: 0 },
      ],
      tableColor, // 表格颜色
      titleColor, // 弹窗颜色
      buttonColor, // 按钮颜色
      list: null, // 查询表单数据
      total: 0, // 表单总条数
      listLoading: true, // 等待查询loading
      listQuery: { // 查询字段
        page: 1,
        limit: 20,
        fsiteNo: undefined,
        ftaskname: undefined,
        fteamno: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      dataForm: { // 添加，编辑字段
        ftaskno: undefined,
        fteamno: undefined,
        ftaskname: undefined,
        fsiteNo: undefined,
        fperson: undefined,
        fmemo: undefined,
        fstate: undefined,
        id: undefined
      },
      dialogFormVisible: false, // 添加，编辑弹窗是否显示
      dialogStatus: '', // 判断弹窗是添加/编辑
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: { // 表单提交规则
        fteamno: [
          { required: true, message: '团队编号不能为空' },
        ],
        ftaskno: [
          { required: true, message: '课题组编号不能为空' },
        ],
        ftaskname: [
          { required: true, message: '课题组名称不能为空' },
        ],
        fperson: [
          { required: true, message: '责任人不能为空' },
        ],
      }
    }
  },
  created() {
    this.getList()
    this.panelHeight = document.documentElement.clientHeight - 378
    this.listQuery.fsiteNo = this.$store.state.user.fsiteNo;
    // 人员
    getListArea({ flag: 13, arg1: this.$store.state.user.fsiteNo })
      .then(response => {
        this.getListforduserAdd = response.data.data.list
      })
      .catch(() => {
        this.getListforduserAdd = []
      })
    // 实验室
    getListArea({ flag: 10, arg1: this.$store.state.user.fsiteNo })
      .then(response => {
        this.getListfpointNo = response.data.data.list
      })
      .catch(() => {
        this.getListfpointNo = []
      })
    // 团队
    listtsTeam({ fsiteNo: this.$store.state.user.fsiteNo, limit: -1 })
      .then(response => {
        this.fteamnoList = response.data.data.list
      })
      .catch(() => {
        this.fteamnoList = []
      })

  },
  methods: {
    //实验室弹出框添加框确定
    updateDataLabSon() {
      this.$refs["dataFormLabSon"].validate((valid) => {
        if (valid) {
          // 调用api添加
          createtsTaskpoint({ ftaskno: this.dataForm.ftaskno, fpointno: this.dataFormLabSon.fpointno })
            .then(response => {
              this.labListTamp.push(this.dataFormLabSon)
              this.dataFormLabSon = {
                fpointno: '',
                fpointname: '',
              }
              this.addLabVisible = false
            })
            .catch((response) => {
              this.$notify.error({
                title: '失败',
                message: response.data.data
              })
            })
        }
      });
    },
    //实验室弹出框添加框打开
    handleUpdatelab() {
      this.addLabVisible = true
    },
    //实验室弹出框添加框删除
    handleDelete2(row) {
      // 调用api删除
      deletetsTaskpoint({ ftaskno: this.dataForm.ftaskno, fpointno: row.fpointno })
        .then(response => {
          const index = this.labListTamp.indexOf(row);
          this.labListTamp.splice(index, 1);
        })
        .catch((response) => {
          this.$notify.error({
            title: '失败',
            message: response.data.data
          })
        })
    },
    // 实验室确定
    updateDataLab() {
      // 调用api添加
      this.dialogLabVisible = false
    },
    // 实验室取消
    cancleLab() {
      this.dialogLabVisible = false
    },
    // 实验室弹窗
    lab(row) {
      this.dataForm = Object.assign({}, row)
      // 查询课题组下的实验室列表
      listtsTaskpoint({ ftaskno: this.dataForm.ftaskno })
        .then(response => {
          this.labListTamp = response.data.data.list
        })
        .catch(() => {
          this.labListTamp = []
        })
      this.dialogLabVisible = true
      this.dialogStatus = 'update'
    },
    //人员弹出框添加框删除
    handleDelete1(row) {
      // 调用api删除
      deletetsTaskuser({ ftaskno: this.dataForm.ftaskno, fuserno: row.id })
        .then(response => {
          const index = this.personList.indexOf(row);
          this.personList.splice(index, 1);
        })
        .catch((response) => {
          this.$notify.error({
            title: '失败',
            message: response.data.data
          })
        })
    },
    // 人员弹出框添加框确定
    updateDataPersonSon() {
      this.$refs["dataFormPersonSon"].validate((valid) => {
        if (valid) {
          // 调用api添加
          createtsTaskuser({ ftaskno: this.dataForm.ftaskno, fuserno: this.dataFormPersonSon.fuserno })
            .then(response => {
              this.personList.push({
                id: this.dataFormPersonSon.fuserno,
                username: this.dataFormPersonSon.fusername,
              })
              this.dataFormPersonSon = {
                fuserno: '',
                fusername: '',
              }
              this.addPersonVisible = false
            })
            .catch((response) => {
              this.$notify.error({
                title: '失败',
                message: response.data.data
              })
            })
        }
      });
    },
    //人员弹出框添加框
    handleUpdatePerson() {
      this.addPersonVisible = true
    },
    // 人员弹出框确定
    updateDataPerson() {
      this.dialogPersonVisible = false
    },
    // 人员弹出框取消
    canclePerson() {
      this.dialogPersonVisible = false
    },
    // 人员弹窗打开
    person(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogPersonVisible = true
      this.dialogStatus = 'update'
      // 查询课题组下的人员列表
      listtsTaskuser({ ftaskno: this.dataForm.ftaskno })
        .then(response => {
          this.personList = response.data.data.list
        })
        .catch(() => {
          this.personList = []
        })
      console.log('this.dataForm', this.dataForm);
    },

    // 搜索地点弹出框
    searchDialoglab(value) {
      this.labFlag = value
      if (value === 'add') {
        this.labDialogFsiteNo = this.listQuery.fsiteNo
      } else {
        this.labDialogFsiteNo = this.listQuery.fsiteNo
      }
      this.searchVisiblelab = true
    },

    // 地点弹出框返回值
    labDialogValue(value) {
      if (this.labFlag === 'add') {
        console.log('value.lab', value.lab);
        this.dataFormLabSon.fpointno = value.lab.fpointNo
        this.dataFormLabSon.fpointname = value.lab.fpointName
      } else {
      }
    },

    // 搜索人员弹出框
    searchDialoguse(value) {
      this.userFlag = value
      if (value === 'add') {
        this.userDialogFsiteNo = this.listQuery.fsiteNo
      } else {
        this.userDialogFsiteNo = this.listQuery.fsiteNo
      }
      this.searchVisibleuse = true
    },

    // 人员弹出框返回值
    userDialogValue(value) {
      if (this.userFlag === 'add') {
        this.dataForm.fperson = value.user.id.toString()
      } else {
        this.dataFormPersonSon.fuserno = value.user.id.toString()
        this.dataFormPersonSon.fusername = value.user.username
      }
    },
    // 团队回显
    fteamnoFormatter: function (row, column) {
      if (this.fteamnoList) {
        for (let i = 0; i < this.fteamnoList.length; i++) {
          if (row.fteamno == this.fteamnoList[i].fteamno) {
            return this.fteamnoList[i].fteamname
          }
        }
        return ''
      } else {
        return ''
      }
    },
    // 弹窗实验室回显
    fpointnoFormatter: function (row, column) {
      if (this.getListfpointNo) {
        for (let i = 0; i < this.getListfpointNo.length; i++) {
          if (row.fpointno == this.getListfpointNo[i].value) {
            return this.getListfpointNo[i].label
          }
        }
        return ''
      } else {
        return ''
      }
    },
    // 弹窗人员回显
    fusernoFormatter: function (row, column) {
      if (this.getListforduserAdd) {
        for (let i = 0; i < this.getListforduserAdd.length; i++) {
          if (row.id == this.getListforduserAdd[i].value) {
            return this.getListforduserAdd[i].label
          }
        }
        return ''
      } else {
        return ''
      }
    },
    // 人员回显
    fpersonFormatter: function (row, column) {
      if (this.getListforduserAdd) {
        for (let i = 0; i < this.getListforduserAdd.length; i++) {
          if (row.fperson == this.getListforduserAdd[i].value) {
            return this.getListforduserAdd[i].label
          }
        }
        return ''
      } else {
        return ''
      }
    },
    // 时间回显
    datetimeFormat(row, column) {
      const date = row[column.property]
      if (date === undefined || date === null) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    // 获取数据
    getList() {
      this.listLoading = true
      listtsTask(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    // 查找
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 添加时重置表单
    resetForm() {
      this.dataForm = {
        ftaskno: undefined,
        fteamno: undefined,
        ftaskname: undefined,
        fsiteNo: undefined,
        fperson: undefined,
        fmemo: undefined,
        fstate: undefined,
        id: undefined
      }
    },
    // 打开添加弹窗
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.dataForm.fstate = 1
      this.dataForm.fsiteNo = this.$store.state.user.fsiteNo;
      this.dataForm.fperson = this.$store.state.user.id.toString()
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 添加
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          createtsTask(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加课题组成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.data
              })
            })
        }
      })
    },
    // 打开编辑弹窗
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 编辑
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updatetsTask(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.ftaskno === this.dataForm.ftaskno) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新课题组成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.data
              })
            })
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('是否要删除课题组', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletetsTask(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除课题组成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.data
            })
          })
      })
    },
    // 表单序号
    indexMethod(index) {
      index = index + 1
      return index
    }
  }
}
</script>
